import {
	defineStore
} from 'pinia'
import {
	computed,
	ref,
	watch
} from 'vue-demi'
// 你可以为`defineStore()`的返回值命名任何你想要的名字，
// 但最好使用商店的名称并用`use`和`Store`包围它
//（例如`useUserStore`、`useCartStore`、`useProductStore` )

// 第一个参数是应用程序中商店的唯一 id
export const usePiniaStore = defineStore('main', () => {
	const base_shu_xing = ref([])
	const user_info = ref({})
	const nature_list = ref([])
	const isLogin = computed(()=>{return user_info.value&&JSON.stringify(user_info.value)!="{}"})
    const wholeLoading = ref(false)
	const wholeLoadingPercent = ref(0)
    const shadeLoading = ref(false)
	const shadeLoadingPercent = ref(0)
	
	watch(wholeLoadingPercent, (newValue, OldValue, onCleanup) => {
		if(newValue>=100){
			WHOLE_LOADING()
		}
		onCleanup(() => {});
	})
	watch(shadeLoadingPercent, (newValue, OldValue, onCleanup) => {
		if(newValue>=100){
			SHADE_LOADING()
		}
		onCleanup(() => {});
	})
	
	
	function UPDATE_USER_INFO(data){
		//更新用户信息
		user_info.value = data
	}
	function INIT_BASE_SHUXING(arr) {
		//获取属性
		base_shu_xing.value = arr
	}
	function UPDATE_NATURE_LIST(arr){
		nature_list.value = arr
	}
	function SHADE_LOADING(bool){
		if(bool){
			shadeLoading.value = true
		}else{
			shadeLoading.value = false
		}
	}
	function WHOLE_LOADING(bool){
		if(bool){
			wholeLoading.value = true
		}else{
			wholeLoading.value = false
		}
	}
	function SHADE_LOADING_PERCENT(percent){
		if(percent<=100){
			shadeLoadingPercent.value = percent
		}else{
			shadeLoadingPercent.value = 0
		}
	}
	function WHOLE_LOADING_PERCENT(percent){
		if(percent<=100){
			wholeLoadingPercent.value = percent
		}else{
			wholeLoadingPercent.value = 0
		}
	}
	
	
	return {
		base_shu_xing,
		user_info,
		nature_list,
		isLogin,
		shadeLoading,
		wholeLoading,
		shadeLoadingPercent,
		wholeLoadingPercent,
		INIT_BASE_SHUXING,
		UPDATE_USER_INFO,
		UPDATE_NATURE_LIST,
		SHADE_LOADING,
		WHOLE_LOADING,
		SHADE_LOADING_PERCENT,
		WHOLE_LOADING_PERCENT,
	}
}, {
	// persist: true //持久化存储
	persist:{  //替换uniapp操作storage的API
		enabled: true,
		storage:{
			getItem(key){
				return uni.getStorageSync(key) 
			},
			setItem(key,val){
				uni.setStorageSync(key,val)
			}
		}
	}
})